package com.feilong.edgeAI.utils;

import android.content.Context;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.ContentValues;
import android.content.Context;
import android.content.res.AssetManager;
import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class FileHelper {

    public static void renameAssetFile(Context context, String assetFileName, String newFileName) {
        File file = context.getFilesDir(); // 获取内部存储的文件目录
        File newFile = new File(file, newFileName); // 创建新文件对象

        try {
            InputStream is = context.getAssets().open(assetFileName); // 打开assets中的原始文件
            OutputStream os = new FileOutputStream(newFile); // 创建目标文件的输出流

            byte[] buffer = new byte[1024];
            int read;
            while ((read = is.read(buffer)) != -1) {
                os.write(buffer, 0, read);
            }

            is.close();
            os.flush();
            os.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void copyAssetFileToInternalStorage(Context context, String assetFileName, String destinationFileName) {
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            inputStream = context.getAssets().open(assetFileName);
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
                // 对于Android 10及以上版本，使用Context.getFilesDir()获取的路径需要自己拼接文件夹
                File fileDir = context.getDataDir(); // 内部存储的应用专属目录
                File outputFile = new File(fileDir, destinationFileName);
                outputStream = new FileOutputStream(outputFile);
            } else {
                // 对于Android 10以下版本，可以直接使用Context.getFilesDir()获取内部存储的文件路径
                File fileDir = context.getFilesDir();
                File outputFile = new File(fileDir, destinationFileName);
                outputStream = new FileOutputStream(outputFile);
            }

            byte[] buffer = new byte[1024];
            int read;
            while ((read = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, read);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }




    public void copyAssetsToExternalStorage(Context context, String assetFolderPath, String targetDirName) {
        AssetManager assetManager = context.getAssets();
        String[] files = null;
        try {
            files = assetManager.list(assetFolderPath);
        } catch (IOException e) {
            e.printStackTrace();
        }

        File externalDir = context.getExternalFilesDir(null);
        File targetDir = new File(externalDir, targetDirName);
        if (!targetDir.exists()) {
            targetDir.mkdirs();
        }

        for (String filename : files) {
            File outputFile = new File(targetDir, filename);
            if (outputFile.exists()) {
                outputFile.delete();
            }
            try (InputStream in = assetManager.open(assetFolderPath + "/" + filename);
                 OutputStream out = new FileOutputStream(outputFile)) {
                byte[] buffer = new byte[1024];
                int read;
                while ((read = in.read(buffer)) != -1) {
                    out.write(buffer, 0, read);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void copyAssetsToExternalStorage(Context context) {
        AssetManager assetManager = context.getAssets();
        File externalFilesDir = context.getExternalFilesDir("");

        if (externalFilesDir != null) {
            try {
                String[] assets = assetManager.list("");
                for (String asset : assets) {
                    String[] path = asset.split("/");
                    String fileName = path[path.length - 1];
                    File assetFile = new File(externalFilesDir, fileName);
                    if (assetFile.exists() && assetFile.length() > 0) {
                        continue; // Skip files that already exist.
                    }
                    InputStream in = assetManager.open(asset);
                    OutputStream out = new FileOutputStream(assetFile);

                    // Transfer bytes from in to out
                    byte[] buf = new byte[1024];
                    int len;
                    while ((len = in.read(buf)) > 0) {
                        out.write(buf, 0, len);
                    }
                    in.close();
                    out.flush();
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void copyAssetFileToExternalStorage(Context context, String fileName) {
        AssetManager assetManager = context.getAssets();
        File externalDir = context.getExternalFilesDir(null); // 获取应用外部文件存储目录
        Log.e("aaaaaaaaaaaaaaaaa",externalDir.getPath());

        if (externalDir != null) {
            File file = new File(externalDir, fileName); // 确定目标文件路径
            try (InputStream is = assetManager.open(fileName);
                 OutputStream os = new FileOutputStream(file)) {
                byte[] buffer = new byte[1024];
                int read;
                while ((read = is.read(buffer)) != -1) {
                    os.write(buffer, 0, read);
                }
            } catch (IOException e) {
                // 处理异常
                e.printStackTrace();
            }
        }
    }

    public void copyAssetToFile(Context context, String assetName, String newName) {
        AssetManager assetManager = context.getAssets();
//        File externalDir = context.getExternalFilesDir(null); // 获取应用外部文件存储目录
        InputStream in = null;
        OutputStream out = null;
        try {
            in = assetManager.open(assetName);
            File targetFile = new File("/storage/emulated/0/Android/data/", newName);
            out = new FileOutputStream(targetFile);

            byte[] buffer = new byte[1024];
            int read;
            while ((read = in.read(buffer)) != -1) {
                out.write(buffer, 0, read);
            }

            // 成功复制
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
